using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using Citrix.Cmp;

namespace SkinningSample.ViewModel
{
    /// <summary>
    /// This is the view-model object for the Page2 view. This page displays a list of books.
    /// </summary>
    class Page2ViewModel : INotifyPropertyChanged, IPage
    {
        /// <summary>
        /// A reference back to the root v-m object.
        /// </summary>
        private readonly ViewModelManager _vmRoot;

        /// <summary>
        /// A list of books to display to the user.
        /// </summary>
        private List<BookViewModel> _books;

        /// <summary>
        /// Initialisation constructor.
        /// </summary>
        /// <param name="vmMgr">The root view-model object.</param>
        /// <exception cref="ArgumentNullException"></exception>
        public Page2ViewModel(ViewModelManager vmMgr)
        {
            if (null == vmMgr)
            {
                throw new ArgumentNullException("vmMgr");
            }

            _vmRoot = vmMgr;
            BackCommand = _vmRoot.DefaultBackCommand;

            _books = new List<BookViewModel>();
            _books.Add(new BookViewModel { Title = "America Again: Re-becoming the Greatness We Never Weren't", Author = "Stephen Colbert", Publisher = "Grand Central Publishing", ISBN = "0446583979" });
            _books.Add(new BookViewModel { Title = "Barefoot Contessa Foolproof: Recipes You Can Trust", Author = "Ina Garten", Publisher = "Clarkson Potter", ISBN = "0307464873" });
            _books.Add(new BookViewModel { Title = "Darth Vader and Son", Author = "Jeffrey Brown", Publisher = "Chronicle Books", ISBN = "145210655X" });
            _books.Add(new BookViewModel { Title = "Dust of Dreams", Author = "Steven Erikson", Publisher = "Tor Fantasy", ISBN = "0765348861" });
            _books.Add(new BookViewModel { Title = "The End of Your Life Book Club", Author = "Will Schwalbe", Publisher = "Knopf", ISBN = "0307594033" });
            _books.Add(new BookViewModel { Title = "The Forgotten", Author = "David Baldacci", Publisher = "Grand Central Publishing", ISBN = "0446573051" });
            _books.Add(new BookViewModel { Title = "How to Tell If Your Cat Is Plotting to Kill You", Author = "Matthew Inman", Publisher = "Andrews McMeel Publishing", ISBN = "1449410243" });
            _books.Add(new BookViewModel { Title = "The Racketeer", Author = "John Grisham", Publisher = "Doubleday", ISBN = "0385535147" });
            _books.Add(new BookViewModel { Title = "The Strange Case of Origami Yoda", Author = "Tom Angleberger", Publisher = "Amulet Books", ISBN = "0810984253" });
            _books.Add(new BookViewModel { Title = "Texts from Dog", Author = "October Jones", Publisher = "Mariner Books", ISBN = "0544077741" });
            _books.Add(new BookViewModel { Title = "Without Remorse", Author = "Tom Clancy", Publisher = "Berkley", ISBN = "0425143325" });
            _books.Add(new BookViewModel { Title = "Underwater Dogs", Author = "Seth Casteel", Publisher = "Little, Brown and Company", ISBN = "0316227706" });
        }

        /// <summary>
        /// Navigates back to the previous page.
        /// </summary>
        public DelegateCommand BackCommand
        {
            get;
            private set;
        }

        /// <summary>
        /// A list of books to display to the user.
        /// </summary>
        public List<BookViewModel> Books
        {
            get { return _books; }
        }

        #region INotifyPropertyChanged Members

        /// <summary>
        /// Fired whenever a public property changes.
        /// </summary>
        public event PropertyChangedEventHandler PropertyChanged;

        /// <summary>
        /// Fires the property change event.
        /// </summary>
        /// <param name="propertyName">The name of the property that changed.</param>
        private void OnPropertyChanged(string propertyName)
        {
            NotifyHelper.VerifyProperty(propertyName, this);
            if (this.PropertyChanged != null)
            {
                this.PropertyChanged.Raise(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        #endregion // INotifyPropertyChanged Members

        #region IPage Members

        /// <summary>
        /// Used to notify the page when is made the active page, or when it has become inactive.
        /// </summary>
        /// <param name="isActive">True if this page is now active, false if it has become
        /// inactive.</param>
        public void NotifyActiveChanged(bool isActive)
        {
        }

        /// <summary>
        /// Notifies the active page that the physical back button has been pressed on the
        /// client device.
        /// </summary>
        public void NotifyBackButtonPressed()
        {
            BackCommand.Execute();
        }

        /// <summary>
        /// Used to access the back command that will navigate you from the current page to the
        /// previous page in the navigation history.
        /// </summary>
        public ViewModelManager VmManager
        {
            get { return this._vmRoot; }
        }

        #endregion
    }
}
